package com.raccoon.ibatis.manager;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class IBatisManagerImpl implements IBatisManager {

	private SqlMapClient	sqlMapper;
	private boolean			enable = false;
	
	/**
	 * 
	 * @param configFile
	 */
	public IBatisManagerImpl(String configFile) {
		// TODO Auto-generated constructor stub
		try {
			Reader reader = Resources.getResourceAsReader(configFile);
			sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
			reader.close();
		} catch (IOException e) {
			// Fail fast.
			throw new RuntimeException("Something bad happened while building the SqlMapClient instance." + e, e);
		}
	}
	
	/**
	 * 
	 */
	public Map selectAll(String id, Map map, String paramKey) throws SQLException {
		return sqlMapper.queryForMap(id, map, paramKey);
	}

	/**
	 * 
	 */
	public List selectAll(String id, Object paramObject) throws SQLException {
		return sqlMapper.queryForList(id, paramObject);
	}

	/**
	 * 
	 */
	public List selectAll(String id) throws SQLException {
		return sqlMapper.queryForList(id);
	}

	@Override
	public boolean isEnable() {
		return enable;
	}

	@Override
	public void setEnable(boolean enable) {
		this.enable = enable;
	}

	/**
	 * 
	 */
	public List selectWithLike(String id, String str) throws SQLException {
		// TODO Auto-generated method stub
		return sqlMapper.queryForList(id, str);
	}

	/**
	 * 
	 */
	public Object selectWithUserKey(String id, int key)
			throws SQLException {
		// TODO Auto-generated method stub
		return sqlMapper.queryForObject(id, key);
	}

	/**
	 * 
	 */
	public Object insert(String id, Object obj) throws SQLException {
		// TODO Auto-generated method stub
		return sqlMapper.insert(id, obj);
	}

	/**
	 * 
	 */
	public int delete(String id, Object obj) throws SQLException {
		// TODO Auto-generated method stub
		return sqlMapper.delete(id, obj);
	}

	/**
	 * 
	 */
	public int update(String id, Object obj) throws SQLException {
		// TODO Auto-generated method stub
		return sqlMapper.update(id, obj);
	}
	
}
